package cn.jbolt.admin.dictionary;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.cache.JBoltDictionaryTypeCache;
import cn.jbolt.core.db.sql.Sql;
import cn.jbolt.core.model.DictionaryType;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.OnlySaasPlatform;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltDictionaryTypeService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Okv;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 字典类型管理接口
 */
@CheckPermission(PermissionKey.DICTIONARY)
@UnCheckIfSystemAdmin
@OnlySaasPlatform
@Path("/api/admin/dictionaryType")
public class DictionaryTypeAdminController extends JBoltApiBaseController {
    @Inject
    JBoltDictionaryTypeService service;

    /**
     * 查询单个数据
     */
    public void index(){
        renderJBoltApiRet(findModelById());
    }

    /**
     * 后台管理条件查询接口
     * @param page
     * @param pageSize
     * @param enable
     * @param keywords
     */
    @JBoltHttpGet
    public void datas(@Para(value = PAGE_PARAM_NAME,defaultValue = "1")Integer page,
                      @Para(value = PAGESIZE_PARAM_NAME,defaultValue = "15")Integer pageSize,
                      @Para(value = ENABLE_PARAM_NAME,defaultValue = "true")Boolean enable,
                      @Para(KEYWORDS_PARAM_NAME)String keywords){
        renderJBoltApiSuccessWithData(service.paginateByKeywords(Sql.KEY_ID,Sql.KEY_DESC_TRIMED, page, pageSize, keywords, "name,type_key", Okv.by(ENABLE_PARAM_NAME,getBoolean(ENABLE_PARAM_NAME,true))));
    }

    /**
     * select radio checkbox可用的Option 列表接口
     * enable = true
     */
    @JBoltHttpGet
    @UnCheck
    public void options(){
        renderJBoltApiSuccessWithData(JBoltDictionaryTypeCache.me.getEnableOptions());
    }

    /**
     * API专用去掉了createTime等基础维护信息的list数据
     * enable = true
     */
    @JBoltHttpGet
    @UnCheck
    public void apiDatas(){
        renderJBoltApiSuccessWithData(JBoltDictionaryTypeCache.me.getEnableApiDatas());
    }

    /**
     * 保存数据
     * @param dictionaryType
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("")DictionaryType dictionaryType){
        renderJBoltApiRet(service.save(dictionaryType));
    }

    /**
     * 更新数据
     * @param dictionaryType
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")DictionaryType dictionaryType){
        renderJBoltApiRet(service.update(dictionaryType));
    }

    /**
     * 删除数据
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void delete(){
        renderJBoltApiRet(service.delete(getLong(0)));
    }

    /**
     * 切换启用状态
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleEnable(){
        renderJBoltApiRet(service.toggleEnable(getLong(0)));
    }

    /**
     * 提供ModelService
     * @return
     */
    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }
}
